//
//  SwiftLearnView.swift
//  TanTan_Kotlin
//
//  Created by EDY on 5/26/25.
//

import SwiftUI

struct SwiftLearnView:View {
    
    @State var tapCount = 0
    @State private var name = ""
    let students = ["Hermione","Harry","Ron"]
    @State private var selectedStudent = 1
    @State private var showingAlert = false
    @State private var checkAmount = ""
    @State private var numberOfPeople = 2
    @State private var tipPercentage = 2
    @State private var isButtonVisible = true
    @State private var nameGF = ["LM","LYH", "ZJJ"]
    
    var body: some View{
        
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("LYH Hello, world!!!")
        }
        .padding()
        //Form表单
        Form{
            Section{
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")

            }
            Section{
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
                Text("Hello,Swift!")
            }
        }.navigationTitle(Text("SwiftUI"))
        //导航栏
        NavigationView {
            Form{
                Section{
                    Text("Hello,Swift!")
                }
            }.navigationTitle(Text("SwiftUI"))
                .navigationBarTitleDisplayMode(.inline)
        }
        //按钮
        Button("Tap Count\(tapCount)"){
            self.tapCount += 1

        }
        //文本框
        Form{
            TextField("Enter your name",text: $name)
            Text("Your name is \(name)")
        }
        //ForEach循环
        Form{
            ForEach(0 ..< 10){
                number in Text("Row \(number)")
            }
        }
        //选择控件
        Picker("Select your student", selection: $selectedStudent){
            ForEach(0 ..< students.count){
                Text(self.students[$0])
            }
        }
        
        //堆栈
        VStack(alignment:.leading, spacing: 20){

            Text("Hello, World!").background(Color.blue)
            Text("Hello, Swift!")
            HStack(spacing: 20){
                Text("First")
                Text("Second")
                Text("Third")

            }.background(Color.red)
            Spacer()
        }
        //背景颜色
        ZStack{
            Color.red.edgesIgnoringSafeArea(.all)
            Text("First")
        }
  //渐变色
    LinearGradient(gradient: Gradient(colors: [.white,.black]), startPoint: .top, endPoint: .bottom)
        //按钮角标
        Button("Edit"){
            print("Button was tapped")
        }
        Button(action: {
            print("Button action")
        }) {
            HStack(spacing: 10){
                Image(systemName: "pencil")
                Text("Edit")
            }
        }

        //警告弹窗
        Button("Show Alert"){
            self.showingAlert = true
        }.alert(isPresented: $showingAlert){
            Alert(title: Text("Hello SwiftUI"), message: Text("This is some detail"), dismissButton: .default(Text("OK")))
        }
        Form{
            Section{
                TextField("Amount", text: $checkAmount).keyboardType(.decimalPad)
            }
            Section{
                Text("$\(checkAmount)")
            }
        }
        
        //多种视图组合
        VStack{
            Text("Welcomt to SwiftUI!").font(.largeTitle)
            HStack{
                Image(systemName: "star.fill")
                Text("SwiftUI Basics").font(.title)
            }

            Text("Hello, SwiftUI!")
            if isButtonVisible {
                Button("Taggle Button"){
                    self.isButtonVisible.toggle()
                }
            }
            List {
                Text("Item1").foregroundColor(.blue)
                Text("Item2").multilineTextAlignment(.center)
                Text("Item3").bold().italic()
            }
            List(students,id: \.self){
                item in Text(item)
            }
        }
        //list删除列表项
        List{
            ForEach (nameGF, id: \.self){
                item in Text(item)
            }.onDelete(perform: delete)
        }

        List {
            ForEach(nameGF,id:\.self){
                item in Text(item)
            }.onMove(perform: move)
        }.navigationBarItems(trailing: EditButton())
    }
    
    func move(from: IndexSet, to: Int) {
        nameGF.move(fromOffsets: from, toOffset: to)
    }
    
    func delete(at offsets: IndexSet){
        nameGF.remove(atOffsets: offsets)
    }
}

#Preview {
    SwiftLearnView()
}

